﻿from google.appengine.ext import webapp
from utilities import * 
from entities import * 
from datetime import date
from google.appengine.api import users  
import datetime
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp                                         
import os       
class Form(webapp.RequestHandler):
    def get(self):
        key = Entities.GetKey()
        NganhNghes = NganhNgheEntities.Get(key,0,0)
        MucLuongs = MucLuongEntities.Get(key,0)
        LoaiHinhs = LoaiHinhEntities.Get(key,0)
        TinhThanhs = TinhThanhEntities.Get(key,0)
        DoiTuongs = DoiTuongEntities.Get(key, 0)
        CapBacs = CapBacEntities.Get(key,0)
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'MucLuong': MucLuongs,
            'NganhNghe': NganhNghes, 
            'LoaiHinh': LoaiHinhs,
			'TinhThanh': TinhThanhs,
			'DoiTuong' : DoiTuongs,
			'CapBac': CapBacs,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()            
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/formPostJob.html')
        self.response.out.write(template.render(path, template_values))

class Sign(webapp.RequestHandler):
    def post(self):
        key = Entities.GetKey()
        NgayKetThuc =  self.request.get('date_end')
        #NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
        ChucDanh =  self.request.get('txtPosition')
        Email =  users.get_current_user().nickname()
        NhaTuyenDungID = NhaTuyenDungEntities.GetCurrent(Email)
        MieuTa =  self.request.get('txtDescription')
        YeuCau =  self.request.get('txtRequire')   
        NganhNgheID =  self.request.get('ddlNganhNghe')
        MucLuongID =  self.request.get('ddlMucLuong')
        LoaiHinhID =  self.request.get('ddlLoaiHinh')
        TinhThanhID = self.request.get('ddlTinhThanh')
        DoiTuongs  = self.request.get_all('ckbDoiTuong')
        CapBacID = self.request.get('ddlCapBac')
        TrangThaiID = TrangThaiEntities.GetID(Entities.GetKey(),u'Vừa nhập');
        NgayDang = datetime.datetime.now()
        status = DonDangTuyenEntities.Add(key, NgayDang, NgayKetThuc, MieuTa, YeuCau, NhaTuyenDungID, NganhNgheID, ChucDanh, MucLuongID, LoaiHinhID, TinhThanhID, CapBacID, TrangThaiID,DoiTuongs)
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'NhaTuyenDung' : NhaTuyenDungID,
            'status': status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()            
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/completeJob.html')
        self.response.out.write(template.render(path, template_values))
        
class Manager(webapp.RequestHandler):
    def get(self):
        key = Entities.GetKey()
        Email =  users.get_current_user().nickname()
        NhaTuyenDungID = NhaTuyenDungEntities.GetCurrent(Email)
        dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),NhaTuyenDungID)
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'dondangtuyen': dondangtuyen,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()                
            #'NhaTuyenDung' : nhatuyendung.TenThuongHieu
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/managerJob.html')
        self.response.out.write(template.render(path, template_values))                   
    def post(self):
        key = Entities.GetKey()
        try:
            Email =  users.get_current_user().nickname()
            NhaTuyenDungID = NhaTuyenDungEntities.GetCurrent(Email)
            dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),NhaTuyenDungID)
            #nhatuyendung = Entities.NhaTuyenDung.get_by_id(NhaTuyenDungID)
            #nhatuyendung = Entitíe.NhaTuyenDung.all().filter(
            #dondangtuyen= Entities.DonDangTuyen.all().filter("NhaTuyenDungID", nhatuyendung)
        
            #dondangtuyen = db.GqlQuery("SELECT * FROM DonDangTuyen WHERE NhaTuyenDungID = :1",
            #                  nhatuyendung)
            #TrangThaiID = 73
            #trangthai = Entities.TrangThaiXetDuyet.get_by_id(TrangThaiID)
            #dondangtuyen = Entities.DonDangTuyen.all().filter("TrangThaiID=", trangthai)
            #key = Entities.GetKey()
            NgayKetThuc =  self.request.get('date_end')
            #NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
            ChucDanh =  self.request.get('txtPosition')
            Email =  users.get_current_user().nickname()
            NhaTuyenDungID = NhaTuyenDungEntities.GetCurrent(Email)
            MieuTa =  self.request.get('txtDescription')
            YeuCau =  self.request.get('txtRequire')   
            NganhNgheID =  self.request.get('ddlNganhNghe')
            MucLuongID =  self.request.get('ddlMucLuong')
            LoaiHinhID =  self.request.get('ddlLoaiHinh')
            TinhThanhID = self.request.get('ddlTinhThanh')
            DoiTuongs  = self.request.get_all('ckbDoiTuong')
            CapBacID = self.request.get('ddlCapBac')
            TrangThaiID = TrangThaiEntities.GetID(Entities.GetKey(),u'Vừa nhập');
            NgayDang = datetime.datetime.now()
            status = DonDangTuyenEntities.Add(key, NgayDang, NgayKetThuc, MieuTa, YeuCau, NhaTuyenDungID, NganhNgheID, ChucDanh, MucLuongID, LoaiHinhID, TinhThanhID, CapBacID, TrangThaiID,DoiTuongs)
           
            #dondangtuyen = dondangtuyen.filter("TrangThaiID", trangthai)
            template_values = {
                'menu' : HtmlTemplate.Menu(),
                'dondangtuyen': dondangtuyen,
                'menu' : HtmlTemplate.Menu(),
                'footer': HtmlTemplate.Footer()                
                #'NhaTuyenDung' : nhatuyendung.TenThuongHieu
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/managerJob.html')
            self.response.out.write(template.render(path, template_values))    
        except:
            return "Bạn không có quyền truy cập trang này "
        #dondangtuyen = dondangtuyen.filter("TrangThaiID=",trangthai) 

class Details(webapp.RequestHandler):
    def get(self):
        DonDangTuyenID = int(self.request.get("job_id"))
        dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),0,DonDangTuyenID,"","","",0,0,0,0,0,0,0,0)
        NhaTuyenDungID = dondangtuyen[0]['NhaTuyenDungID']
        nhatuyendung = NhaTuyenDungEntities.Get(Entities.GetKey(),"", "", 0, 0, 0, NhaTuyenDungID)
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'DonDangTuyen': dondangtuyen,
            'menu' : HtmlTemplate.Menu(),
            'NhaTuyenDung': nhatuyendung,
            'footer': HtmlTemplate.Footer()            
            }        
        
        path = os.path.join(os.path.dirname(__file__), '../templates/detailJob.html')
        self.response.out.write(template.render(path, template_values))  

class Update(webapp.RequestHandler):
    def get(self):
        key = Entities.GetKey()
        DonDangTuyenID = int(self.request.get("job_id"))
        dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),0,DonDangTuyenID,"","","",0,0,0,0,0,0,0,0)
        doituong_dangtuyen = DangTuyenDoiTuongEntities.Get(Entities.GetKey(),DonDangTuyenID)
        NganhNghes = NganhNgheEntities.Get(key,0,0)
        MucLuongs = MucLuongEntities.Get(key,0)
        LoaiHinhs = LoaiHinhEntities.Get(key,0)
        TinhThanhs = TinhThanhEntities.Get(key,0)
        DoiTuongs = DoiTuongEntities.Get(key, 0)
        CapBacs = CapBacEntities.Get(key,0)
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'MucLuong': MucLuongs,
            'NganhNghe': NganhNghes, 
            'LoaiHinh': LoaiHinhs,
			'TinhThanh': TinhThanhs,
			'DoiTuong' : DoiTuongs,
			'CapBac': CapBacs,
            'DoiTuongDangTuyen' : doituong_dangtuyen,
            'dondangtuyen' : dondangtuyen,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()            
            } 
        path = os.path.join(os.path.dirname(__file__), '../templates/updateJob.html')
        self.response.out.write(template.render(path, template_values))  
        
    def post(self):
        DonDangTuyenID = int(self.request.get("job_id"))        
        key = Entities.GetKey()
        #Email =  users.get_current_user().nickname()
        #NhaTuyenDungID = NhaTuyenDungEntities.GetCurrent(Email)
        dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),0,DonDangTuyenID,"","","",0,0,0,0,0,0,0,0)
        #key = Entities.GetKey()
        NgayKetThuc =  self.request.get('date_end')
        #NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
        ChucDanh =  self.request.get('txtPosition')
        MieuTa =  self.request.get('txtDescription')
        YeuCau =  self.request.get('txtRequire')   
        NganhNgheID =  self.request.get('ddlNganhNghe')
        MucLuongID =  self.request.get('ddlMucLuong')
        LoaiHinhID =  self.request.get('ddlLoaiHinh')
        TinhThanhID = self.request.get('ddlTinhThanh')
        DoiTuongs  = self.request.get_all('ckbDoiTuong')
        CapBacID = self.request.get('ddlCapBac')
        #TrangThaiID = TrangThaiEntities.GetID(Entities.GetKey(),u'Vừa nhập');
        status = DonDangTuyenEntities.Update(Entities.GetKey(),dondangtuyen.DonDangTuyenID, NgayKetThuc, MieuTa, YeuCau, NhaTuyenDungID, NganhNgheID, ChucDanh, MucLuongID, LoaiHinhID, TinhThanhID, CapBacID)
        for dtuong in DoiTuongs:
            DangTuyenDoiTuongEntitities.Add(Entities.GetKey(), dondangtuyen.DonDangTuyenID, dtuong)
        self.redirect("/managerJob")
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()            
            }        
        path = os.path.join(os.path.dirname(__file__), '../templates/updateJob.html')
        self.response.out.write(template.render(path, template_values))    
        
class Search(webapp.RequestHandler):
    def get(self):
        key = Entities.GetKey()
        NganhNghes = NganhNgheEntities.Get(key,0,0)
        MucLuongs = MucLuongEntities.Get(key,0)
        LoaiHinhs = LoaiHinhEntities.Get(key,0)
        TinhThanhs = TinhThanhEntities.Get(key,0)
        DoiTuongs = DoiTuongEntities.Get(key, 0)
        CapBacs = CapBacEntities.Get(key,0)    
        TrangThaiID = TrangThaiEntities.GetID(key,u"Hợp lệ")
        DonDangTuyens = DonDangTuyenEntities.Get(key,0,0,"","","",0,0,TrangThaiID,0,0,0,0,0)
        #NhaTuyenDungID=0 , DonDangTuyenID=0, txtSearch = "", NgayDg="", NgayKT="", TinhThanhID = 0, CapBacID = 0, TrangThaiID=0, MucLuongID = 0, LoaiHinhID=0, LinhVucID = 0, NganhNgheID=0, DoiTuongID = 0
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'MucLuong': MucLuongs,
            'NganhNghe': NganhNghes, 
            'LoaiHinh': LoaiHinhs,
			'TinhThanh': TinhThanhs,
			'DoiTuong' : DoiTuongs,
			'CapBac': CapBacs,
            'DonDangTuyen' : DonDangTuyens,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()            
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/searchJob.html')
        self.response.out.write(template.render(path, template_values))
    def post(self):
        key = Entities.GetKey()
        NganhNghes = NganhNgheEntities.Get(key,0,0)
        MucLuongs = MucLuongEntities.Get(key,0)
        LoaiHinhs = LoaiHinhEntities.Get(key,0)
        TinhThanhs = TinhThanhEntities.Get(key,0)
        DoiTuongs = DoiTuongEntities.Get(key, 0)
        CapBacs = CapBacEntities.Get(key,0)    
        TrangThaiID = TrangThaiEntities.GetID(key,u"Hợp lệ")
        
        #NgayKetThuc =  self.request.get('date_end')
        ChucDanh =  self.request.get('txtPosition') 
        NganhNgheID =  self.request.get('ddlNganhNghe')
        MucLuongID =  self.request.get('ddlMucLuong')
        LoaiHinhID =  self.request.get('ddlLoaiHinh')
        TinhThanhID = self.request.get('ddlTinhThanh')
        DoiTuongID  = self.request.get('ddlDoiTuong')
        CapBacID = self.request.get('ddlCapBac')
        
        DonDangTuyens = DonDangTuyenEntities.Get(key,0,0,ChucDanh,"","",int(TinhThanhID),int(CapBacID),TrangThaiID,int(MucLuongID),int(LoaiHinhID),0,int(NganhNgheID),int(DoiTuongID))
        #NhaTuyenDungID=0 , DonDangTuyenID=0, txtSearch = "", NgayDg="", NgayKT="", TinhThanhID = 0, CapBacID = 0, TrangThaiID=0, MucLuongID = 0, LoaiHinhID=0, LinhVucID = 0, NganhNgheID=0, DoiTuongID = 0
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'MucLuong': MucLuongs,
            'NganhNghe': NganhNghes, 
            'LoaiHinh': LoaiHinhs,
			'TinhThanh': TinhThanhs,
			'DoiTuong' : DoiTuongs,
			'CapBac': CapBacs,
            'DonDangTuyen' : DonDangTuyens,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()            
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/searchJob.html')
        self.response.out.write(template.render(path, template_values))